package com.atguigu.utils;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.StringUtils;

import com.aspose.cells.Cell;
import com.aspose.cells.Cells;
import com.aspose.cells.Row;
import com.aspose.cells.RowCollection;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
import com.aspose.cells.WorksheetCollection;
import com.aspose.slides.Presentation;
import com.aspose.words.Document;

public abstract class AsposeOfficeUtil {
	/**
	 * 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
	 * 
	 * @param from
	 * @param to
	 * @throws Exception
	 */
	public static String word(String from, String to) throws Exception {
		if (!getWordLicense())
			throw new Exception("com.aspose.words lic ERROR!");

		String suffixStr = to.substring(to.lastIndexOf(".") + 1);
		int suffix = com.aspose.words.SaveFormat.UNKNOWN;
		if (suffixStr.equals("doc")) {
			suffix = com.aspose.words.SaveFormat.DOC;
		} else if (suffixStr.equals("docx")) {
			suffix = com.aspose.words.SaveFormat.DOCX;
		} else if (suffixStr.equals("pdf")) {
			suffix = com.aspose.words.SaveFormat.PDF;
		} else if (suffixStr.equals("html")) {
			suffix = com.aspose.words.SaveFormat.HTML;
		} else if (suffixStr.equals("txt")) {
			suffix = com.aspose.words.SaveFormat.TEXT;
		}
		try {
			// long old = System.currentTimeMillis();
			Document doc = new Document(from); // Address是将要被转化的word文档
			doc.save(to, suffix); // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
			// System.out.println("共耗时：" + ((System.currentTimeMillis() - old) / 1000.0) +
			// "秒"); // 转化用时
		} catch (Exception e) {
			e.printStackTrace();
		}
		return to;
	}

	public static String word(InputStream from, String to) throws Exception {
		if (!getWordLicense())
			throw new Exception("com.aspose.words lic ERROR!");

		String suffixStr = to.substring(to.lastIndexOf(".") + 1);
		int suffix = com.aspose.words.SaveFormat.UNKNOWN;
		if (suffixStr.equals("doc")) {
			suffix = com.aspose.words.SaveFormat.DOC;
		} else if (suffixStr.equals("docx")) {
			suffix = com.aspose.words.SaveFormat.DOCX;
		} else if (suffixStr.equals("pdf")) {
			suffix = com.aspose.words.SaveFormat.PDF;
		} else if (suffixStr.equals("html")) {
			suffix = com.aspose.words.SaveFormat.HTML;
		} else if (suffixStr.equals("txt")) {
			suffix = com.aspose.words.SaveFormat.TEXT;
		}
		try {

			Document doc = new Document(from); // Address是将要被转化的word文档
			doc.save(to, suffix); // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
			from.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return to;
	}

	public static boolean word(InputStream from, OutputStream to, String suffixStr) throws Exception {
		boolean flag = false;
		if (!getWordLicense())
			throw new Exception("com.aspose.words lic ERROR!");
		int suffix = com.aspose.words.SaveFormat.UNKNOWN;
		if (suffixStr.equals("doc")) {
			suffix = com.aspose.words.SaveFormat.DOC;
		} else if (suffixStr.equals("docx")) {
			suffix = com.aspose.words.SaveFormat.DOCX;
		} else if (suffixStr.equals("pdf")) {
			suffix = com.aspose.words.SaveFormat.PDF;
		} else if (suffixStr.equals("html")) {
			suffix = com.aspose.words.SaveFormat.HTML;
		} else if (suffixStr.equals("txt")) {
			suffix = com.aspose.words.SaveFormat.TEXT;
		}

		try {
			Document doc = new Document(from); // Address是将要被转化的word文档
			doc.save(to, suffix); // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
			flag = true;
			from.close();
			to.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return flag;
	}

	public static String excel(String from, String to) throws Exception {
		if (!getExcelLicense())
			throw new Exception("com.aspose.cells lic ERROR!");

		String suffixStr = to.substring(to.lastIndexOf(".") + 1);
		int suffix = com.aspose.cells.SaveFormat.UNKNOWN;
		if (suffixStr.equals("xlsx")) {
			suffix = com.aspose.cells.SaveFormat.XLSX;
		} else if (suffixStr.equals("xls")) {
			suffix = com.aspose.cells.SaveFormat.EXCEL_97_TO_2003;
		} else if (suffixStr.equals("pdf")) {
			suffix = com.aspose.cells.SaveFormat.PDF;
		} else if (suffixStr.equals("html")) {
			suffix = com.aspose.cells.SaveFormat.HTML;
		} else if (suffixStr.equals("csv")) {
			suffix = com.aspose.cells.SaveFormat.CSV;
		} else if (suffixStr.equals("svg")) {
			suffix = com.aspose.cells.SaveFormat.SVG;
		} else {
			suffix = com.aspose.cells.SaveFormat.AUTO;
		}

//		File file = new File(to);// 输出路径
		try {
			long old = System.currentTimeMillis();

			Workbook wb = new Workbook(from);// 原始excel路径
//			FileOutputStream fileOS = new FileOutputStream(file);
			wb.save(to, suffix);
//			fileOS.close();
			System.out.println("共耗时：" + ((System.currentTimeMillis() - old) / 1000.0) + "秒"); // 转化过程耗时
		} catch (Exception e) {
			e.printStackTrace();
		}
		// return file.getName();
		return to;
	}

	public static String excel(InputStream from, String to) throws Exception {
		if (!getExcelLicense())
			throw new Exception("com.aspose.cells lic ERROR!");

		String suffixStr = to.substring(to.lastIndexOf(".") + 1);
		int suffix = com.aspose.cells.SaveFormat.UNKNOWN;
		if (suffixStr.equals("xlsx")) {
			suffix = com.aspose.cells.SaveFormat.XLSX;
		} else if (suffixStr.equals("xls")) {
			suffix = com.aspose.cells.SaveFormat.EXCEL_97_TO_2003;
		} else if (suffixStr.equals("pdf")) {
			suffix = com.aspose.cells.SaveFormat.PDF;
		} else if (suffixStr.equals("html")) {
			suffix = com.aspose.cells.SaveFormat.HTML;
		} else if (suffixStr.equals("csv")) {
			suffix = com.aspose.cells.SaveFormat.CSV;
		} else if (suffixStr.equals("svg")) {
			suffix = com.aspose.cells.SaveFormat.SVG;
		} else {
			suffix = com.aspose.cells.SaveFormat.AUTO;
		}

		try {
			Workbook wb = new Workbook(from);// 原始excel路径
			wb.save(to, suffix);
			from.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return to;
	}

	public static boolean excel(InputStream from, OutputStream to, String suffixStr) throws Exception {
		boolean flag = false;
		if (!getExcelLicense())
			throw new Exception("com.aspose.cells lic ERROR!");
		int suffix = com.aspose.cells.SaveFormat.UNKNOWN;
		if (suffixStr.equals("xlsx")) {
			suffix = com.aspose.cells.SaveFormat.XLSX;
		} else if (suffixStr.equals("xls")) {
			suffix = com.aspose.cells.SaveFormat.EXCEL_97_TO_2003;
		} else if (suffixStr.equals("pdf")) {
			suffix = com.aspose.cells.SaveFormat.PDF;
		} else if (suffixStr.equals("html")) {
			suffix = com.aspose.cells.SaveFormat.HTML;
		} else if (suffixStr.equals("csv")) {
			suffix = com.aspose.cells.SaveFormat.CSV;
		} else if (suffixStr.equals("svg")) {
			suffix = com.aspose.cells.SaveFormat.SVG;
		} else {
			suffix = com.aspose.cells.SaveFormat.AUTO;
		}

		try {
			// long old = System.currentTimeMillis();
			Workbook wb = new Workbook(from);// 原始excel路径
			wb.save(to, suffix);
			from.close();
			to.close();
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	public static String ppt(String from, String to) throws Exception {
		// 验证License
		if (!getPPTLicense()) {
			throw new Exception("com.aspose.slides lic ERROR!");
		}
		File file = new File(to);
		File fileParent = file.getParentFile();
		// 文件夹不存在则创建文件夹
		if (!fileParent.isDirectory()) {
			fileParent.mkdirs();
		}
		String suffixStr = to.substring(to.lastIndexOf(".") + 1);
		int suffix = 0;
		if (suffixStr.equals("ppt")) {
			suffix = com.aspose.slides.SaveFormat.Ppt;
		} else if (suffixStr.equals("pptx")) {
			suffix = com.aspose.slides.SaveFormat.Pptx;
		} else if (suffixStr.equals("pdf")) {
			suffix = com.aspose.slides.SaveFormat.Pdf;
		} else if (suffixStr.equals("html")) {
			suffix = com.aspose.slides.SaveFormat.Html;
		} else if (suffixStr.equals("swf")) {
			suffix = com.aspose.slides.SaveFormat.Swf;
		}
//		File file = new File(to);// 输出pdf路径
		try {
			long old = System.currentTimeMillis();
			Presentation pres = new Presentation(from);// 输入pdf路径
//			FileOutputStream fileOS = new FileOutputStream(file);
			pres.save(to, suffix);
//			fileOS.close();
			System.out.println("共耗时：" + ((System.currentTimeMillis() - old) / 1000.0) + "秒"); // 转化过程耗时

		} catch (Exception e) {
			e.printStackTrace();
		}
		// return file.getName();
		return to;
	}

	public static String ppt(InputStream from, String to) throws Exception {
		// 验证License
		if (!getPPTLicense())
			throw new Exception("com.aspose.slides lic ERROR!");

		String suffixStr = to.substring(to.lastIndexOf(".") + 1);
		int suffix = 0;
		if (suffixStr.equals("ppt")) {
			suffix = com.aspose.slides.SaveFormat.Ppt;
		} else if (suffixStr.equals("pptx")) {
			suffix = com.aspose.slides.SaveFormat.Pptx;
		} else if (suffixStr.equals("pdf")) {
			suffix = com.aspose.slides.SaveFormat.Pdf;
		} else if (suffixStr.equals("html")) {
			suffix = com.aspose.slides.SaveFormat.Html;
		} else if (suffixStr.equals("swf")) {
			suffix = com.aspose.slides.SaveFormat.Swf;
		}
//		File file = new File(to);// 输出pdf路径
		try {
			// long old = System.currentTimeMillis();
			Presentation pres = new Presentation(from);// 输入pdf路径
//			FileOutputStream fileOS = new FileOutputStream(file);
			pres.save(to, suffix);
			from.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		// return file.getName();
		return to;
	}

	public static boolean ppt(InputStream from, OutputStream to, String suffixStr) throws Exception {
		boolean flag = false;
		// 验证License
		if (!getPPTLicense())
			throw new Exception("com.aspose.slides lic ERROR!");
		int suffix = 0;
		if (suffixStr.equals("ppt")) {
			suffix = com.aspose.slides.SaveFormat.Ppt;
		} else if (suffixStr.equals("pptx")) {
			suffix = com.aspose.slides.SaveFormat.Pptx;
		} else if (suffixStr.equals("pdf")) {
			suffix = com.aspose.slides.SaveFormat.Pdf;
		} else if (suffixStr.equals("html")) {
			suffix = com.aspose.slides.SaveFormat.Html;
		} else if (suffixStr.equals("swf")) {
			suffix = com.aspose.slides.SaveFormat.Swf;
		}
//		File file = new File(to);// 输出pdf路径
		try {
			// long old = System.currentTimeMillis();
			Presentation pres = new Presentation(from);// 输入pdf路径
//			FileOutputStream fileOS = new FileOutputStream(file);
			pres.save(to, suffix);
			from.close();
			to.close();
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		// return file.getName();
		return flag;
	}

	private static boolean getWordLicense() {
		boolean result = false;
		try {
			InputStream is = AsposeOfficeUtil.class.getClassLoader().getResourceAsStream("license.xml");
			com.aspose.words.License aposeLic = new com.aspose.words.License();
			aposeLic.setLicense(is);
			result = true;
		} catch (Exception e) {

		}
		return result;
	}

	private static boolean getExcelLicense() {
		boolean result = false;
		try {
			InputStream is = AsposeOfficeUtil.class.getClassLoader().getResourceAsStream("license.xml");
			com.aspose.cells.License aposeLic = new com.aspose.cells.License();
			aposeLic.setLicense(is);
			result = true;
		} catch (Exception e) {

		}
		return result;
	}

	private static boolean getPPTLicense() {
		boolean result = false;
		try {
			InputStream is = AsposeOfficeUtil.class.getClassLoader().getResourceAsStream("license.xml");
			com.aspose.slides.License aposeLic = new com.aspose.slides.License();
			aposeLic.setLicense(is);
			result = true;
		} catch (Exception e) {

		}
		return result;
	}

	public static void main(String[] args) throws Exception {
		// System.out.println(getWordLicense());
		// System.out.println(getExcelLicense());
		// System.out.println(getPPTLicense());
		// word("E:/office/pdf/xlsx2pdf.html", "E:/office/pdf/333.txt");
		// excel("E:/office/333.xlsx", "E:/office/pdf/333.html");
		ppt("D:/files/有序用电-采集负控/src/B01D6E1FB3BA4A84A7C38CD30962EAEA1585631247.pptx",
				"D:/files/有序用电-采集负控/pdf/B01D6E1FB3BA4A84A7C38CD30962EAEA1585631247.pdf");
	}

	@SuppressWarnings("unchecked")
	public static String office2String(InputStream from, String suffixStr) throws Exception {
		StringBuffer strb = new StringBuffer();
		if (suffixStr.trim().equals("docx") || suffixStr.trim().equals("doc")) {
			Document doc = new Document(from);
			strb.append(doc.getText());
		}

		if (suffixStr.trim().equals("xlsx") || suffixStr.trim().equals("xls")) {
			Workbook wb = new Workbook(from);
			WorksheetCollection wc = wb.getWorksheets();
			Worksheet ws = wc.get(0);
			Cells cell = ws.getCells();
			Iterator<Row> it = cell.getRows().iterator();
			while (it.hasNext()) {
				Iterator<Cell> itc = it.next().iterator();
				while (itc.hasNext()) {
					strb.append(itc.next().getDisplayStringValue() + "\t");
				}
				strb.append("\n");
			}
		}
		return strb.toString();
	}

	public static List<Map<String, Object>> excel2list(String Filepatrh) {
		List<Map<String, Object>> list = null;
		try {
			Workbook wb = new Workbook(Filepatrh);
			WorksheetCollection wcs = wb.getWorksheets();
			Worksheet ws = wcs.get(0);
			Cells cells = ws.getCells();
			RowCollection rows = cells.getRows();
			list = excel2list(rows);
		} catch (Exception e) {
			e.getStackTrace();
		}
		return list;
	}

	public static List<Map<String, Object>> excel2list(InputStream File) {
		List<Map<String, Object>> list = null;
		try {
			Workbook wb = new Workbook(File);
			WorksheetCollection wcs = wb.getWorksheets();
			Worksheet ws = wcs.get(0);
			Cells cells = ws.getCells();
			RowCollection rows = cells.getRows();
			list = excel2list(rows);
		} catch (Exception e) {
			e.getStackTrace();
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	private static List<Map<String, Object>> excel2list(RowCollection rows) {
		Iterator<Row> itr = rows.iterator();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		while (itr.hasNext()) {
			boolean flat = false;
			int type = 0, plevel = 0, score = 0, isOrder = 1;
			String mold = "", stem = "", trued = "", analysis = "", pa = "", pb = "", pc = "", pd = "", pe = "",
					pf = "";
			Map<String, Object> map = new LinkedHashMap<String, Object>();
			Row row = itr.next();
			if (row.getIndex() == 0) {
				continue;
			}
			Iterator<Cell> itc = row.iterator();
			while (itc.hasNext()) {
				Cell cell = itc.next();
				int i = cell.getColumn();
				if (i > 13) {
					flat = true;
					continue;
				}
				String str = cell.getDisplayStringValue();
				int[] coul = new int[] { 0, 1, 2, 4, 11 };
				for (int j : coul) {
					if (j == i && StringUtils.isEmpty(str)) {
						flat = true;
						continue;
					}
				}

				if (str == null || "".equals(str) || "null".equals(str))
					str = "";

				switch (i++) {
				case 0:
					if (str.trim().equals("单选题"))
						type = 1;
					if (str.trim().equals("多选题"))
						type = 2;
					if (str.trim().equals("判断题"))
						type = 3;
					if (str.trim().equals("填空题"))
						type = 4;
					if (str.trim().equals("简答题"))
						type = 5;
					break;
				case 1:
					mold = str.trim();
					break;
				case 2:
					if (str.trim().equals("简单"))
						plevel = 1;
					if (str.trim().equals("一般"))
						plevel = 2;
					if (str.trim().equals("困难"))
						plevel = 3;
					break;
				case 3:
					if (str.trim().equals(""))
						score = 0;
					else
						score = Integer.valueOf(str.trim());
					break;
				case 4:
					stem = str.trim();
					break;
				case 5:
					pa = str.trim();
					break;
				case 6:
					pb = str.trim();
					break;
				case 7:
					pc = str.trim();
					break;
				case 8:
					pd = str.trim();
					break;
				case 9:
					pe = str.trim();
					break;
				case 10:
					pf = str.trim();
					break;
				case 11:
					if (str.trim().equals("正确"))
						trued = "1";
					else if (str.trim().equals("错误"))
						trued = "0";
					else
						trued = str.trim();
					break;
				case 12:
					analysis = str.trim();
					break;
				case 13:
					if (str.trim().equals(""))
						isOrder = 1;
					else
						isOrder = Integer.valueOf(str.trim());
					break;
				}
			}
			if (flat) {
				continue;
			}
			map.put("type", type);
			map.put("mold", mold);
			map.put("plevel", plevel);
			map.put("score", score);
			map.put("stem", stem);
			map.put("pa", pa);
			map.put("pb", pb);
			map.put("pc", pc);
			map.put("pd", pd);
			map.put("pe", pe);
			map.put("pf", pf);
			map.put("trued", trued);
			map.put("analysis", analysis);
			map.put("isOrder", isOrder);
			list.add(map);
		}
		return list;

	}

}
